package com.media.selector.media.helper

import android.content.ContentResolver
import android.content.Context
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.MediaStore
import android.view.View
import androidx.annotation.NonNull
import androidx.annotation.Nullable
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.core.os.EnvironmentCompat
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import java.io.File
import java.io.IOException
import java.text.SimpleDateFormat
import java.util.*

/**
 * date        ：2020/5/15
 * author      ：蒙景博
 * description ：
 */

/**
 * 设置 Toolbar
 */
internal fun AppCompatActivity.setToolbar(@NonNull toolbar: Toolbar, @Nullable callback: (() -> Unit)? = null) {
    setSupportActionBar(toolbar)
    supportActionBar?.let {
        it.setDisplayHomeAsUpEnabled(true)
        it.setDisplayShowTitleEnabled(false)
    }
    toolbar.setNavigationOnClickListener { callback?.invoke() }
}

/**
 * 使用 ViewPager2 时快速创建 FragmentStateAdapter
 */
internal fun AppCompatActivity.createViewPagerAdapter(@NonNull fragments: List<Fragment>) = object : FragmentStateAdapter(supportFragmentManager, this.lifecycle) {
    override fun getItemCount(): Int = fragments.size
    override fun createFragment(position: Int): Fragment = fragments[position]
}

/**
 * 设置当前View全屏展示
 */
internal const val FLAGS_FULLSCREEN =
    View.SYSTEM_UI_FLAG_LOW_PROFILE or
            View.SYSTEM_UI_FLAG_FULLSCREEN or
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
            View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or
            View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION



object MediaPath {

    private const val SCHEME_CONTENT = "content"

    fun getPath(resolver: ContentResolver, uri: Uri?): String? {
        if (uri == null) return null
        if (SCHEME_CONTENT == uri.scheme) {
            resolver.query(uri, arrayOf(MediaStore.Images.ImageColumns.DATA), null, null, null)?.use {
                return if (!it.moveToFirst()) null else it.getString(it.getColumnIndex(MediaStore.Images.ImageColumns.DATA))
            }
        }
        return uri.path
    }

    /**
     * 获取文件地址
     * Uri 转 File
     */
    fun getFilePath(@NonNull context: Context, @Nullable uri: Uri?): String? {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            getAndroidQVersionFilePath(context, uri)
        } else getAndroidQPreviousVersionFilePath(context, uri)
    }

    /**
     * AndroidQ URI转化文件路径
     */
    @RequiresApi(Build.VERSION_CODES.Q)
    private fun getAndroidQVersionFilePath(@NonNull context: Context, @Nullable uri: Uri?): String? {
        if (uri == null) return null
        val scheme = uri.scheme
        var data: String? = null
        when {
            scheme.isNullOrEmpty() -> data = uri.path
            ContentResolver.SCHEME_FILE == scheme -> data = uri.path
            ContentResolver.SCHEME_CONTENT == scheme -> {
                val projection = arrayOf(MediaStore.Images.ImageColumns.RELATIVE_PATH, MediaStore.Images.Media.DISPLAY_NAME)
                context.contentResolver.query(uri, projection, null, null, null)?.use {
                    if (it.moveToFirst()) {
                        val name = it.getString(it.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME))
                        val path = it.getString(it.getColumnIndex(MediaStore.Images.Media.RELATIVE_PATH))
                        data = "$path$name"
                    }
                }
            }
        }
        return data
    }

    /**
     * AndroidQ以前版本，URI转化文件路径
     */
    private fun getAndroidQPreviousVersionFilePath(@NonNull context: Context, @Nullable uri: Uri?): String? {
        if (uri == null) return null
        val scheme = uri.scheme
        var data: String? = null
        when {
            scheme == null -> data = uri.path
            ContentResolver.SCHEME_FILE == scheme -> data = uri.path
            ContentResolver.SCHEME_CONTENT == scheme -> {
                val projection = arrayOf(MediaStore.Images.ImageColumns.DATA)
                context.contentResolver.query(uri, projection, null, null, null)?.use {
                    if (it.moveToFirst()) {
                        val index = it.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
                        if (index > -1) {
                            data = it.getString(index)
                        }
                    }
                }
            }
        }
        return data
    }
}